package com.drug.service.impl;

import com.drug.domain.Hospital;
import com.drug.domain.Result;
import com.drug.mapper.HospitalMapper;
import com.drug.service.HospitalService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HospitalServiceImpl implements HospitalService {

     @Autowired
     private HospitalMapper hospitalMapper;

     // 多条件查询医院
    @Override
    public List<Hospital> selectHospital(Hospital hospital) {
        //医院名称
        if (hospital.gethName()!=null&&hospital.gethName()!=""){
            String name = hospital.gethName();
            hospital.sethName(name.toString());
        }else{
            hospital.sethName(null);
        }
        //邮政编码
        if (hospital.gethPostalcode()!=null&&hospital.gethPostalcode()!=""){
            String postalcode = hospital.gethPostalcode();
            hospital.sethPostalcode(postalcode.toString());
        }else{
            hospital.sethPostalcode(null);
        }
        //医院级别
        if (hospital.gethGrade()!=null&&hospital.gethGrade()!=""){
            String grade = hospital.gethGrade();
            hospital.sethGrade(grade.toString());
        }else{
            hospital.sethGrade(null);
        }
        //是否营利机构
        if (hospital.gethProfit()!=null||hospital.gethProfit().equals("")){
            String profit = hospital.gethProfit();
            hospital.sethProfit(profit);
        }else{
            hospital.sethProfit(null);
        }
        //医院类型
        if (hospital.gethHcategory()!=null||hospital.gethHcategory().equals("")){
            String hcategory = hospital.gethHcategory();
            hospital.sethHcategory(hcategory);
        }else {
            hospital.sethHcategory(null);
        }
        List<Hospital> list = hospitalMapper.selectHospital(hospital);
        return list;
    }
    //分页查询
    @Override
    public Result pageFindAll(Integer page) {
        //使用分页插件
        PageHelper.startPage(page,5);
        Result result = new Result<>();
        List<Hospital> hospitals = hospitalMapper.pageFindAll();
        PageInfo<Hospital> pageInfo = new PageInfo<>(hospitals);
        result.setData(pageInfo);
        return result;
    }
    //逻辑删除医院
    @Override
    public void deleteHospital(Integer hId) {
        hospitalMapper.deleteHospital(hId);
    }

    //添加医院
    @Override
    public void insertHospital(Hospital hospital) {
        hospitalMapper.insertHospital(hospital);
    }

    //修改医院
    @Override
    public List<Hospital> updateHospital(Hospital hospital) {
        hospitalMapper.updateHospital(hospital);
        return null;
    }

    /**
     * 查询医院名
     * @param hName
     * @return
     */
    @Override
    public Result selectHname(String hName) {
        Result result = new Result();
        String[] hospitals = hospitalMapper.selectHname(hName);
        for (int i = 0; i < hospitals.length; i++) {
            if (hospitals[i].equals(hName)) {
                result.setMsg("医院名已存在");
                result.setCode(0);
                break;
            } else {
                result.setMsg("医院名可用");
                result.setCode(1);
            }
        }
        return result;
    }



}
